 package com.question.service.serviceFactory;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.Properties;

import com.question.dao.StudentDao;
import com.question.dao.daoFactory.DaoFactory;
import com.question.domain.Topic;
import com.question.service.AcademyService;
import com.question.service.StudentService;
import com.question.service.TopicService;

public class ServiceFactory {
	private static StudentService studentService = null;
	private static TopicService topicService = null;
	private static AcademyService academyService = null;
	
	private static ServiceFactory instance = new ServiceFactory();
	
	private ServiceFactory() {
		Properties properties = new Properties();
		InputStream is = null;
		String studentServiceClass = null;
		String topicServiceClass = null;
		String academyServiceClass = null;
		
		try {
			is = ServiceFactory.class.getClassLoader().getResourceAsStream("serivce.properties");
			properties.load(is);
			
			studentServiceClass = properties.getProperty("studentSerivce");
			topicServiceClass = properties.getProperty("topicSerivce");
			academyServiceClass = properties.getProperty("academyService");
			
			Class<StudentService> class1 = (Class<StudentService>) Class.forName(studentServiceClass);
			Constructor<StudentService> con1 = class1.getDeclaredConstructor();
			studentService = con1.newInstance();
			
			Class<TopicService> class2 = (Class<TopicService>) Class.forName(topicServiceClass);
			Constructor<TopicService> con2 = class2.getDeclaredConstructor();
			topicService = con2.newInstance();
			
			Class<AcademyService> class3 = (Class<AcademyService>) Class.forName(academyServiceClass);
			Constructor<AcademyService> con3 = class3.getDeclaredConstructor();
			academyService = con3.newInstance();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static ServiceFactory getInstance() {
		return instance;
	}
	public StudentService getStudentService() {
		return studentService;
	}
	public AcademyService getAcademyService(){
		return academyService;
	}
	public TopicService geTopicService() {
		return topicService;
	}

}
